#include <cstdio>
#include "gui/itemviewdetailsdialog.h"
#include "core/constants.h"

void ItemViewDetailsDialog::initDialogComponents()
{
    QFont headerFont, font;
    mainLayout = new QVBoxLayout;

    nameLabel = new QLabel;
    headerFont = nameLabel->font();
    headerFont.setPointSize(font.pointSize() + HEADER_UPSIZE);
    nameLabel->setFont(headerFont);
    mainLayout->addWidget(nameLabel);

    codeLabel = new QLabel;
    mainLayout->addWidget(codeLabel);
    mainLayout->addWidget(new SeparatorWidget);

    supplierLabel = new QLabel;
    mainLayout->addWidget(supplierLabel);

    srpLabel = new QLabel;
    mainLayout->addWidget(srpLabel);

    stockBox = new QGroupBox("Stocks");
    stockLayout = new QVBoxLayout;
    stockView = new StockView(this, item);
    stockLayout->addWidget(stockView);
    stockBox->setLayout(stockLayout);
    mainLayout->addWidget(stockBox);

    mainLayout->addStretch();

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    buttonBox->addButton("&Edit...", QDialogButtonBox::AcceptRole);
    mainLayout->addWidget(buttonBox);

    this->setLayout(mainLayout);
}

void ItemViewDetailsDialog::initSignalsAndSlots()
{
    connect(this->buttonBox, SIGNAL(accepted()),
            this, SLOT(accept()));
    connect(this->buttonBox, SIGNAL(rejected()),
            this, SLOT(reject()));
}

void ItemViewDetailsDialog::setItem(ItemModel *item)
{
    this->nameLabel->setText(item->getName());
    this->codeLabel->setText(item->getCode());
    this->supplierLabel->setText(QString("Supplied by ") + item->getSupplier()->getName());
    this->item = item;

    char buf[16];
    snprintf(buf, 16, "SRP: PhP%.2f", item->getSrp());

    this->srpLabel->setText(QString(buf));

    this->stockCtrl->getStocksOf(item);
}

ItemViewDetailsDialog::ItemViewDetailsDialog(QWidget *parent, ItemModel *item) :
    QDialog(parent)
{
    this->setWindowTitle("Item Details");
    this->setMinimumSize(640, 480);
    this->item = item;

    this->initDialogComponents();
    this->initSignalsAndSlots();

    stockCtrl = new StockController;
    this->stockView->setController(stockCtrl);

    this->setItem(item);
}
